mod app;
mod config;
mod message;
mod utils;

use app::MqttApp;
use config::Config;
use eframe;
use utils::setup_custom_fonts;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::load().unwrap_or_else(|e| {
        eprintln!("Failed to load config: {}", e);
        Config::default()
    });

    let options = eframe::NativeOptions {
        window_builder: Some(Box::new(move |builder| {
            builder.with_inner_size(egui::vec2(config.ui.window_width, config.ui.window_height))
                  .with_title("MQTT Client")
                  .with_min_inner_size(egui::vec2(config.ui.min_window_width, config.ui.min_window_height))
        })),
        follow_system_theme: true,
        default_theme: if config.ui.dark_mode {
            eframe::Theme::Dark
        } else {
            eframe::Theme::Light
        },
        ..Default::default()
    };
    
    eframe::run_native(
        "MQTT Client",
        options,
        Box::new(|cc| {
            setup_custom_fonts(&cc.egui_ctx);
            Box::new(MqttApp::default())
        }),
    )?;
    Ok(())
}